package com.hongpu.config;

import com.alibaba.fastjson2.JSONObject;
import com.hongpu.domain.SysUser;
import com.hongpu.vo.SysMenuVo;
import com.hongpu.vo.SysRoleVo;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 张玉胜
 * @since 2025年05月14日
 */
@Component
public class SecurityContextUtil {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    private static StringRedisTemplate staticStringRedisTemplate;
    @PostConstruct
    private void init(){
        staticStringRedisTemplate = stringRedisTemplate;
    }
    private static  String getRedisKey(String username){
        StringBuffer redisKey = new StringBuffer();
        redisKey.append("auth:user:account:").append(username);
        return redisKey.toString();
    }

    private static SecurityContext getSecurityContext(){
        SecurityContext securityContext = SecurityContextHolder.getContext();
        return securityContext;
    }


    /**
     * 获取登录用户的账号
     * @return
     */
    public static String getUserAccount(){
        return getSecurityContext().getAuthentication().getPrincipal().toString();
    }
    /**
     * 获取登录用户
     * @return
     */
    public static SysUser getUser(){
        String redisKey = getRedisKey(getUserAccount());
        String paramSysUser  = staticStringRedisTemplate.opsForValue().get(redisKey).toString();
        if (paramSysUser == null){
            return null;
        }
        SysUser sysUser = JSONObject.parseObject(paramSysUser, SysUser.class);
        return sysUser;
    }

    /**
     * 登录用户的ID
     * @return
     */
    public static Long getUserId(){
        SysUser user = getUser();
        if(user == null){
            return null;
        }else {
            return user.getId();
        }
    }
    /**
     * 登录用户的用户名
     * @return
     */
    public static String getUserName(){
        SysUser user = getUser();
        if(user == null){
            return null;
        }else {
            return user.getUserName();
        }
    }

    /**
     * 登录用户的租户ID
     * @return
     */
    public static Long getTenantId(){
        SysUser user = getUser();
        if(user == null){
            return null;
        }else {
            return user.getTenantId();
        }
    }
    /**
     * 登录用户的手机号
     */
    public static String getPhone(){
        SysUser user = getUser();
        if(user == null){
            return null;
        }else {
            return user.getPhone();
        }
    }
    /**
     * 登录用户的邮箱
     */
    public static String getEmail(){
        SysUser user = getUser();
        if(user == null){
            return null;
        }else {
            return user.getEmail();
        }
    }
    /**
     * 登录用户的角色ID
     */
    public static List<Long> getRoleIds(){
        SysUser user = getUser();
        if(user == null){
            return null;
        }else {
            return user.getRoleIds();
        }
    }
    /**
     * 登录用户的权限ID
     */
    public static List<SysMenuVo> getMenuVoList(){
        SysUser user = getUser();
        if(user == null){
            return null;
        }else {
            return user.getMenuVoList();
        }
    }
}